﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using WebApplication5.Models;

namespace WebApplication5.Controllers
{
    public class HomeController : Controller
    {
        TravelwebsiteEntities db = new TravelwebsiteEntities();
        // GET: Home首页
        public ActionResult Index()
        {
            ViewData["TravelRoutes"] = db.TravelRoutes.ToList();
            
            ViewData["Strategies"] = db.Strategies.ToList();
            return View();
        }
        //详情页
        public ActionResult Xiangqing(int? id)
        {
            Strategies strategies = db.Strategies.SingleOrDefault(f => f.StrategyId == id);
            ViewBag.Title1 = strategies.Title;
            ViewBag.PublishDate = strategies.PublishDate;
            ViewBag.RouteOverview = strategies.RouteOverview;
            ViewBag.GameGuide = strategies.GameGuide;
            return View();
        }

        //旅游攻略
        public ActionResult Blog()
        {
            ViewData["Strategies"] = db.Strategies.ToList();
            return View();
        }
        //精选路线
        public ActionResult Work()
        {
            ViewData["TravelRoutes"] = db.TravelRoutes.ToList();
            return View();
        }
        //联系我们
        public ActionResult Contact()
        {
            return View();
        }


        //关于我们
        public ActionResult About()
        {
            return View();
        }

        //私人定制
        public ActionResult Services()
        {
            return View();
        }

        //登陆
        public ActionResult Login()
        {
            return View();
        }


        //退出登陆
        public ActionResult EneDogin()
        {
            if (Session["username"] == null)
            {
                return Redirect("/Home/Index");

            }
            else
            {
                Session["username"] = null;
                return Content(" <script>alert('退出成功')\r\n;location.href='/Home/Index'</script>");

            }

        }

        //判断登陆
        public ActionResult DoLogin(string username, string pwd)
        {
            if (username == "" || pwd == "")
            {
                return Content(" <script>alert('账号密码为空请输入账号密码')\r\n;location.href='/Home/Login'    </script>");
            }
            else
            {
                int count = db.Users.Where(p => p.Username == username && p.Passwords == pwd).Count();
                if (count > 0)
                {
                    Session["username"] = username;
                    return Redirect("/Home/Index");

                }
                else
                {
                    return Content(" <script>alert('账号密码不正确')\r\n;location.href='/Home/Login'</script>");
                }
            }
        }
        //注册
        public ActionResult Zhuce(Users users)
        {
            if (Request["Passwords2"] == Request["Passwords"])
            {

                db.Users.Add(users);
                if (db.SaveChanges() > 0)
                {
                    return Content(" <script>alert('注册成功')\r\n;location.href='/Home/Login'</script>");
                }
                else
                {
                    return Content(" <script>alert('注册失败，请重新注册！！')\r\n;location.href='/Home/Login'</script>");

                }
            }
            else
            {
                return Content(" <script>alert('两次密码不一致')\r\n;location.href='/Home/Login'</script>");
            }


        }




        //购买旅游路线门票
        public ActionResult Gouwuche(int? id)
        {
            ViewData["Comments"] = db.Comments.ToList();

            if (Session["username"] == null)
            {
                return View();

            }
            else
            {
                string name = Session["username"].ToString();
                Users users = db.Users.SingleOrDefault(p => p.Username == name);
                int userid = users.UserId;
                TravelRoutes travelRoutes = db.TravelRoutes.SingleOrDefault(f => f.RouteId == id);
                ViewBag.travelRoutesName = travelRoutes.Name;
                ViewBag.RouteId = travelRoutes.RouteId;
                ViewBag.Miaoshu = travelRoutes.Miaoshu;
                ViewBag.prc = travelRoutes.prc;
                ViewBag.Price = travelRoutes.Price;
                return View();
            }
        }


        public ActionResult Gou(int?id)
        {
            if (Session["username"] == null)
            {
                return Content(" <script>alert('你还没有登陆请先登录')\r\n;location.href='/Home/Login'    </script>");
            }
            else
            {
                string name = Session["username"].ToString();
                Users users = db.Users.SingleOrDefault(p => p.Username == name);
                TravelRoutes travelRoutes = db.TravelRoutes.SingleOrDefault(s => s.RouteId == id);
                int userid = users.UserId;
              
                Orders orders = new Orders();
                orders.UserId = userid;
                orders.Payment = travelRoutes.Price;
                orders.RouteId = id;
                orders.OrderDate = DateTime.Now;
                orders.UserId = users.UserId;
               
                db.Orders.Add(orders);
                if (db.SaveChanges() > 0)
                {
                    Session["ordersid"] =orders.OrderId;
                  
                    return Content("<script>alert('加入成功')\r\n;location.href='/Home/Zhongxin'</script>");
                }
                else
                {
                    return Content("<script>alert('加入失败')\r\n;location.href='/Home/Gouwuche'</script>");
                }

            }


        }

        public ActionResult Pinglun(string CommentText)
        {
            string username = Session["username"].ToString();
            Users users = db.Users.SingleOrDefault(p => p.Username == username);
            Comments comments = new Comments();
            comments.CommentDate = DateTime.Now;
            comments.UserId = users.UserId;
            comments.CommentText = CommentText;
            db.Comments.Add(comments);
            if (db.SaveChanges() > 0)
            {
                return Content("<script>alert('评论成功')\r\n;location.href='/Home/Index'</script>");

            }
            else
            {
                return Content("<script>alert('评论失败，重新评论')\r\n;location.href='/Home/Gouwuche'</script>");

            }
        }



        //酒店
        public ActionResult Hotel()
        {
            ViewData["Hotel"] = db.Hotel.ToList();
            return View();
        }
        public ActionResult Jiudian(int? id)
        {
            Session["id"] = id;
            return View();
        }
        public ActionResult AddJiudian(HotelRooms hotelRooms)
        {
            if (Session["username"] == null)
            {
                return Content("<script>alert('您还没有登陆')\r\n;location.href='/Home/Login'</script>");
            }
            else
            {
                if (hotelRooms != null)
                {
                    string username = Session["username"].ToString();
                    Users users = db.Users.SingleOrDefault(u => u.Username == username);
                    int id = Convert.ToInt32(Session["id"]);
                    hotelRooms.HotelID = id;
                    hotelRooms.Userld = users.UserId;
                    Hotel hotel = db.Hotel.SingleOrDefault(s => s.HotelId == hotelRooms.HotelID);

                    db.HotelRooms.Add(hotelRooms);
                    if (db.SaveChanges() > 0)
                    {
                        return Content("<script>alert('预定成功')\r\n;location.href='/Home/Hotel'</script>");
                    }
                    else
                    {
                        return Content("<script>alert('预定失败')\r\n;location.href='/Home/Hotel'</script>");
                    }
                }
                else
                {
                    return Content("<script>alert('请输入信息才能预定哦')\r\n;location.href='/Home/Hotel'</script>");
                }
            }


        }

        //个人中心
        public ActionResult Zhongxin()
        {
            string username = Session["username"].ToString();
            Users users = db.Users.SingleOrDefault(u => u.Username == username);
            ViewBag.Username = users.Username;
            ViewBag.ProfileImage = users.ProfileImage;
            ViewBag.Email = users.Email;
            ViewBag.Address = users.Address;
            ViewBag.Gender = users.Gender;
            ViewBag.Phone = users.Phone;
            ViewBag.Birthdate = users.Birthdate;
            //酒店订单
            ViewData["Orders"] = db.Orders.Where(p => p.UserId == users.UserId).ToList();
            return View();
        }
        //修改密码
        public ActionResult XiuPwd()
        {
            string name = Convert.ToString(Session["username"]);
            Users users = db.Users.FirstOrDefault(p => p.Username == name);
            string Password = Request["Password"];//原密码
            string Password1 = Request["Password1"];//新密码
            string Password2 = Request["Password2"];//确认密码
            if (Password != users.Passwords)
            {
                return Content("<script>alert('原密码不正确');location.href='/Home/Zhongxin';</script>");
            }
            else
            {

                if (Password2 == Password1)
                {
                    users.Passwords = Password1;
                    db.SaveChanges();
                    return Content("<script>alert('修改成功');location.href='/Home/Zhongxin';</script>");
                }
                else
                {
                    return Content("<script>alert('修改失败，确认密码不等于新密码');location.href='/Home/Zhongxin';</script>");

                }


            }

        }
        //修改资料
        public ActionResult XiuZiliao()
        {
            string name = Convert.ToString(Session["username"]);
            Users users = db.Users.FirstOrDefault(p => p.Username == name);
            users.Username = Request["Username"];
            users.Gender = Request["Gender"];
            users.Email = Request["Email"];
            users.Address = Request["Address"];
            users.Phone = Request["Phone"];
            users.Birthdate = DateTime.Parse(Request["Birthdate"]);
            if (db.SaveChanges() > 0)
            {
                Session["username"] = users.Username;
                return Content("<script>alert('修改成功');location.href='/Home/Zhongxin';</script>");
            }
            else
            {
                return Content("<script>alert('修改失败，重新修改');location.href='/Home/Zhongxin'</script>");
            }
        }
        //路线订单
        public ActionResult CxDingdan()
        {
            db.Configuration.ProxyCreationEnabled = false;
            string username = Session["username"].ToString();
            Users users = db.Users.SingleOrDefault(s => s.Username == username);
            int userid = users.UserId;
            var query = from o in db.Orders
                        join u in db.Users on o.UserId equals u.UserId
                        join t in db.TravelRoutes on o.RouteId equals t.RouteId
                        where u.UserId == userid//条件
                        select new
                        {
                            OrderId = o.OrderId,
                            OrderDate = o.OrderDate.ToString(),
                            UserName = u.Username,
                            Name = t.Name,
                            Price = t.Price,
                            StartPoint = t.StartPoint,
                            Days = t.Days,
                            prc = t.prc,


                        };
            //db.Orders.SingleOrDefault(f=>f.==)
            return Json(query, JsonRequestBehavior.AllowGet);
        }

        public ActionResult JiuDingdan()
        {
            db.Configuration.ProxyCreationEnabled = false;

            string username = Session["username"].ToString();

            Users users = db.Users.SingleOrDefault(s => s.Username == username);
            int userid = users.UserId;
            var query = from f in db.HotelRooms
                        join t in db.Hotel on f.HotelID equals t.HotelId
                        join s in db.HotelType on f.HotelTypeId equals s.HotelTypeId

                        where f.Userld == userid//条件
                        select new
                        {
                            RoomID = f.RoomID,
                            HotelName = t.HotelName,
                            Address=t.Address,
                            Checkintime= f.Checkintime.ToString(),
                            Checkin= f.Checkin.ToString(),
                            HotelImage= t.HotelImage,
                            NumOfBeds=f.NumOfBeds,
                            UserName = f.Username,
                            TypeName= s.TypeName,
                            HotelPirce=t.HotelPirce,
                        };
            //db.Orders.SingleOrDefault(f=>f.==)
            return Json(query, JsonRequestBehavior.AllowGet);
        }
        //删除
        public ActionResult DelDingdan()
        {
            db.Configuration.ProxyCreationEnabled = false;
            int id = int.Parse(Request["id"]);
            var query = db.Orders.SingleOrDefault(o => o.OrderId == id);
            db.Orders.Remove(query);
            db.SaveChanges();
            return Json(query, JsonRequestBehavior.AllowGet);
        }

        //酒店删除
        public ActionResult DelJiuDingdan()
        {
            db.Configuration.ProxyCreationEnabled = false;
            int id = int.Parse(Request["id"]);
            var query = db.HotelRooms.SingleOrDefault(o => o.RoomID == id);
            db.HotelRooms.Remove(query);
            db.SaveChanges();
            return Json(query, JsonRequestBehavior.AllowGet);
        }
    }
}